package games.mapacman.common;

import games.mapacman.client.GameScreen;
import games.mapacman.server.MaPacmanZone;
import marauroa.common.Log4J;
import marauroa.common.game.RPClass;
import marauroa.common.game.RPObject;
import marauroa.common.game.Definition.Type;
import marauroa.server.game.Statistics;

public class Powerpill extends Dot {
	private static final marauroa.common.Logger logger = Log4J
			.getLogger(Powerpill.class);

	private int powerprob = 1;
	private int power_score;
	private boolean isPowerpill;

	public Powerpill(int x, int y, int dot_score, int power_score,
			int power_probability, MaPacmanZone zone) {
		super(x, y, dot_score, zone);
		myObject.put("type", Constants.TYPE_POWERPILL);
		myObject.put("powerprob", power_probability);
		myObject.put("score", power_score);
		myObject.put("dottype", TYPE_POWERPILL);
		powerprob = power_probability;
		this.power_score = power_score;
		isPowerpill = true;
	}

	public Powerpill(RPObject object) {
		super(object);
		isPowerpill = (myObject.getInt("dottype") == TYPE_POWERPILL);
	}

	public static void initRPClass() {
		RPClass superdot = new RPClass(Constants.TYPE_POWERPILL);
		superdot.isA(Constants.TYPE_DOT);
		superdot.addAttribute("dottype", Type.INT);
		superdot.addAttribute("powerprob", Type.INT);
	}

	@Override
	public void setRespawnTime() {
		if (zone.getRandom().nextInt(powerprob) == 0) { // Powerpill
			myObject.put("score", power_score);
			myObject.put("dottype", TYPE_POWERPILL);
			setRespawnTime(zone.getPowerpillRespawnTime());
		} else {
			myObject.put("dottype", TYPE_DOT);
			myObject.put("score", dot_score);
			setRespawnTime(zone.getDotRespawnTime());
		}
	}

	@Override
	public void addStats(Statistics stats) {
		if (myObject.getInt("dottype") == TYPE_POWERPILL) {
			stats.add("Powerpill eaten", 1);
		} else {
			super.addStats(stats);
		}
	}

	@Override
	public void draw(GameScreen screen) {
		if (isPowerpill) {
			screen.draw(screen.getDotSprite(TYPE_POWERPILL), x, y);
		} else {
			screen.draw(screen.getDotSprite(TYPE_DOT), x, y);
		}
	}

	@Override
	public void eaten(RPObject object) {
		if (isPowerpill) {
			object.add("power", 100);
			logger.debug("MaPacman::Powerpill::eaten by " + object);
		}

	}
}
